package se.penselnsvag.bookdb.module.user
{
	import flash.events.Event;
	
	import mx.controls.Alert;
	import mx.modules.Module;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import se.penselnsvag.bookdb.tech.rpc.DataUpdateProxy;
	
	public class UserControl extends Module
	{
		public function UserControl()
		{
		}

		public function saveUser(e:Event):void {
			var u:User = new User();
			u.handle=form.handleTxt.text;
			u.f_name=form.f_nameTxt.text;
			u.l_name=form.l_nameTxt.text;
			var dup:DataUpdateProxy = new DataUpdateProxy(userSaved, fault);
			UserService.getInstance().saveUser(dup, u);
		}
		
		protected function userSaved(e:ResultEvent):void {
			currentState=null;
			form.formHeading.label="Selected user";
		}
		
		public function addUser(e:Event):void {
			currentState="addUser";	
			form.formHeading.label="Add user:";
			clearUserForm();		
		} 
		
		private function fault(fe:FaultEvent):void {
			Alert.show(fe.fault.rootCause.toString(), fe.message.toString());
		}
		
		private function clearUserForm():void { 
			form.handleTxt.text="";
			form.f_nameTxt.text="";
			form.l_nameTxt.text="";			
		}
		
		private function get form():UserForm {
			return UserForm(this);
		}
	}
}